<?php
//
class Admin extends CI_Controller
{ // this controller inherits from CI_Controller

	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('admin_model');
	}


	
	public function index()
	{
		// required
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('session');
		$this->load->helper('cookie');
		
		// Check cookie before validating
		// Check if 'remember_login' cookie IS set 
		//
		if (get_cookie('remember_login')) 
		{
			redirect('admin/entries','refresh');
		}
		
		// Check session before validating
		// Check if login session IS set 
		//
		$age_validation = $this->session->userdata('login_validation');
		//
		if ($age_validation) 
		{
			redirect('admin/entries','refresh');
		}
		
		// validation
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); // check_database callback
		$this->form_validation->set_rules('remember_login', 'Remember Login', 'trim'); // does not ouput error but preserves checkbox state (checked | unchecked)
		//
		$this->form_validation->set_error_delimiters('<li>', '</li>');
		

		// Show form is login has not been validated ('login_validation' session has not been set yet)
		//
		if ($this->form_validation->run() === FALSE)
		{
			// If errors, load form view 
			//
			$data['title'] = 'Login';
			$this->load->view('templates/header_blank', $data);
			$this->load->view('admin/index', $data);
			$this->load->view('templates/footer_blank', $data);
		}		

	}
	
	
	
	function check_database() // called for 'set_rules' 
	{
		// call database on model
		$check = $this->admin_model->check_login();
		//
		if( $check === FALSE )
		{
			// login does NOT match
			//
			$this->form_validation->set_message('check_database', 'Invalid username or password'); 
			//
			return FALSE;
		}
		else
		{
			// login matches
			
			// set cookie if checkbox IS checked
			//
			$checked = $this->input->post('remember_login');
			if((int) $checked == 1)
			{
				$cookie = array(
					'name'   => 'remember_login',
					'value'  => '1',
					'expire' => '31536000', //seconds (1 year)
					//'domain' => '.some-domain.com',
					//'path'   => '/',
					//'prefix' => 'cbb_',
					//'secure' => TRUE
				);
				$this->input->set_cookie($cookie); 
			}
			
			// set 'login_validation' session if form validates correctly
			//
			$this->session->set_userdata(array(
							'login_validation'       => 'TRUE',
							));
			
			// If no errors, redirect to entries
			//
			redirect('admin/entries','refresh');
			
			//
			return TRUE;
		}
	}
	
	
	
	// entries view
	//
	public function entries()
	{
		// get data from model
		//
		$data['title'] = 'Entries';
		$this->load->view('templates/header_admin', $data);
		$this->load->view('admin/entries', $data);
		$this->load->view('templates/footer_blank');
	}

	
	
	
}